var api = require('../../api.js');
var app = getApp();
var is_no_more = false;
var is_loading = false;
var p = 2;
// pages/ph-user.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    total_money: "0.00",
    start_date: app.getDay(-7),
    end_date: app.getDay(0),
    order_list: [],
    page: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.pageOnLoad(this);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var page = this;
    page.getStaffOrderList();
  },
  seraCh: function () {
    this.setData({
      page: 1
    });
    this.getStaffOrderList();
  },
  getStaffOrderList: function () {
    var page = this;
    //wx.showNavigationBarLoading();
    app.request({
      url: api.user.staff_ps_order_list,
      data: { page: page.data.page, start_date: page.data.start_date, end_date: page.data.end_date},
      success: function (res) {
        if (res.code == 0) {
          page.setData({
            order_list: res.data.list,
            page: res.data.page_count + 1,
            total_money: res.data.total_money
          });
        }
        if(page.data.total_money == null) {
          page.setData({
            total_money: 0
          });
        }
        console.log("配送数据列表:" + res.data.list);
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
      }
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindStartDateChange: function (e) {
    this.setData({
      start_date: e.detail.value
    })
  },
  bindEndDateChange: function (e) {
    this.setData({
      end_date: e.detail.value
    })
  },
  click: function () {
    // 允许从相机和相册扫码
    wx.scanCode({
      success: (res) => {
        console.log(res.result)
        var code_data = res.result
        app.request({
          url: api.user.staff_add_ps_order,
          data: { code_data: code_data },
          success: function (res) {
            if (res.code == 0) {
              wx.showToast({
                title: res.msg,
                icon: 'none',
                duration: 2000
              });
              setTimeout(function () {
                wx.navigateTo({
                  url: 'ps-user',
                })
              }, 2000);
            } else {
              wx.showToast({
                title: res.msg,
                icon: 'none',
                duration: 2000
              })
            }
          }
        });
      },
      fail: (res) => {
        wx.showToast({
          title: '扫码失败',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  wancheng: function (e){
    var id = e.currentTarget.dataset.id;
    var page = this;
    app.request({
      url: api.user.staff_check_ps_order,
      data: { status: 1,id:id },
      success: function (res) {
        if (res.code == 0) {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          });
          /*setTimeout(function () {
            wx.navigateTo({
              url: 'ps-user',
            })
          }, 2000);*/
          page.setData({
            page: 1
          });
          page.getStaffOrderList();
          console.log("刷新数据了");
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    });
  },
  goto: function (e) {
    var page = this;
    wx.getSetting({
      success: function (res) {
        if (!res.authSetting['scope.userLocation']) {
          app.getauth({
            content: '需要获取您的地理位置授权，请到小程序设置中打开授权！',
            cancel: false,
            success: function (res) {
              if (res.authSetting['scope.userLocation']) {
                page.location(e);
              }
            }
          });
        } else {
          page.location(e);
        }
      }
    })
  },

  location: function (e) {
    var page = this;
    var address = e.currentTarget.dataset.address;
    var lat = e.currentTarget.dataset.lat;
    var lng = e.currentTarget.dataset.lng;
    console.log("lat:"+lat);
    console.log("lng:" + lng);
    if (lat == 0 || lng == 0){
      wx.showToast({
        title: '位置错误',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    wx.openLocation({
      latitude: lat,
      longitude: lng,
      name: address,
      address: address,
    })
  },
  tel: function (e) {
    var mobile = e.currentTarget.dataset.mobile;
    wx.makePhoneCall({
      phoneNumber: mobile,
    })
  }
})